<template>
  <div class="login-container">
    <!-- 标题导航栏 -->
    <van-nav-bar title="登录" left-arrow />
    <van-form @submit="onSubmit" ref="loginForm">
      <!-- 手机号文本框 -->
      <van-field
        v-model="user.mobile"
        name="mobile"
        :rules="userRules.mobile"
        placeholder="请输入手机号"
      >
        <i slot="left-icon" class="iconfont icon-yonghu"></i>
      </van-field>
      <!-- 验证码输入框 -->
      <van-field
        v-model="user.code"
        type="number"
        name="验证码"
        :rules="userRules.code"
        placeholder="请输入验证码"
      >
        <i slot="left-icon" class="iconfont icon-xiaoxi"></i>
        <template #button>
          <!-- 倒计时器 -->
          <van-count-down
            :time="time"
            format="ss 秒"
            v-if="isShow"
            @finish="isShow = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="default"
            native-type="button"
            @click="sendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from "@/api/user.js";
export default {
  name: "LoginPage",
  props: {},
  data() {
    return {
      time: 5000,
      isShow: false,
      user: {
        // 手机号
        mobile: "",
        // 验证码
        code: 246810,
      },
      // 表单验证规则
      userRules: {
        mobile: [
          { required: true, message: "请填写手机号", trigger: "onBlur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "手机号不合法",
            trigger: "onBlur",
          },
        ],
        code: [
          {
            pattern: /^\d{6}$/,
            message: "验证码不合法",
            trigger: "onBlur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 登录方法
    async onSubmit() {
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: "加载中...",
        forbidClick: true, // 是否禁止背景点击
      });
      // 提示登录进行中
      try {
        let res = await login(this.user.mobile, this.user.code);
        console.log("登录成功", res);
        this.$toast.success("登录成功：)");
        // 保存两个token到本地和Vuex仓库
        this.$store.commit("setUserToken", res.data.data);
        // 跳转到 #/my 组件
        // this.$router.push("/");
        this.$router.back();
      } catch (error) {
        console.log(error);
        this.$toast.fail("登录失败：(");
      }
    },
    // 发送短信验证码
    async sendSms() {
      try {
        await this.$refs.loginForm.validate("mobile");
        this.$toast.success("发送短信验证码成功");
      } catch (error) {
        this.$toast.fail("发送短信验证码失败");
      }
    },
  },

  computed: {},
  watch: {},
  components: {},
};
</script>

<style lang="less" scoped>
.iconfont {
  font-size: 16.5px;
}
// .login-container {
//   border: 1px solid #000;
//   /deep/ ul > li {
//     border: 1px solid #000;
//   }
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  width: 160px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 20px;
  color: #666;
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>